$(document).ready(function() {
	$("#submit_btn").bind("click", saveRooms);

	/*
	 * var company = new CompanyObj("#pp_company").add(new
	 * ComunityObj("#community",{select: function(){
	 * $("#building").attr("disabled",false);
	 * $("#building").selectDiv("val",{communityId:$("#community").attr("data-id")});
	 * },init: function(){ $("#building").attr("disabled",true);
	 * },clear:function(){ $("#building").attr("disabled",true); }}));
	 * company.complete();
	 */
	if (!StrUtil.isEmpty($("#community").attr("data-cy-id"))) {
		getBuildingTypes($("#community").attr("data-cy-id"));
	}
	var initObj = {
		company : {},
		community : {},
		building : {}
	};
	initObj.company.companyId = $("#community").attr("data-cp-id");
	initObj.company.companyName = $("#community").attr("data-cp-name");
	initObj.company.isNeed = true;
	initObj.community.communityId = $("#community").attr("data-cy-id");
	initObj.community.communityName = $("#community").attr("data-cy-name");
	initObj.community.isNeed = true;
	initObj.building.isNeed = true;
	initObj.handler = s_handler;
	s_init(initObj);
	$("#community").on("click", function() {
		s_show();
	});
});
function s_handler(data) {
	var str = "";
	if (!StrUtil.isEmpty(data.company.companyId)) {
		$("#community").attr("data-cp-id", data.company.companyId);
		$("#community").attr("data-cp-name", data.company.companyName);
		str += data.company.companyName;
	}
	if (!StrUtil.isEmpty(data.community.communityId)) {
		$("#community").attr("data-cy-id", data.community.communityId);
		$("#community").attr("data-cy-name", data.community.communityName);
		str += "-" + data.community.communityName;
	}
	if (!StrUtil.isEmpty(data.building.buildingId)) {
		$("#community").attr("data-bd-id", data.building.buildingId);
		$("#community").attr("data-bd-name", data.building.buildingName);
		str += "-" + data.building.buildingName;
	}
	if (!StrUtil.isEmpty(data.unit.unitId)) {
		$("#community").attr("data-un-id", data.unit.unitId);
		$("#community").attr("data-un-name", data.unit.unitName);
		str += "-" + data.building.buildingName;
	}
	if (!StrUtil.isEmpty(data.room.roomId)) {
		$("#community").attr("data-rm-id", data.room.roomId);
		$("#community").attr("data-rm-name", data.room.roomNbr);
		str += "-" + data.room.roomNbr;
	}
	// $("#community").val(str);
	clickOk();
}

function clickOk() {
	if ($("#community").attr("data-bd-id") == null
			|| $("#community").attr("data-bd-id") == "null"
			|| typeof ($("#community").attr("data-bd-id")) == "undefined") {
		$.sucmodal("提示", "请选择楼栋/单元");
		return;
	}
	$("#desCompany").text($("#community").attr("data-cp-name"));
	$("#desCommunity").text($("#community").attr("data-cy-name"));
	if (StrUtil.isEmpty($("#community").attr("data-un-id"))) {
		$("#desBuilding").text($("#community").attr("data-bd-name"));
	} else {
		$("#desBuilding").text(
				$("#community").attr("data-bd-name") + '-'
						+ $("#community").attr("data-un-name"));
	}

	getBuildingTypes($("#community").attr("data-cy-id"));
}
function addRow(obj) {
	$(obj).parents("#rooms a").attr("disabled", "true");
	var cloneRow = $(obj).parents(".room-row").clone();

	$("#rooms").append(cloneRow);
}
function removeRow(obj) {
	var len = $("#rooms .room-row").length;
	if (len != 1) {
		$(obj).parents(".room-row").remove();
	}
}
function saveRooms() {
	if (StrUtil.isEmpty($("#community").attr("data-bd-id"))) {
		$.sucmodal("提示", "请选择楼栋");
		return;
	}
	var buildingId = $("#community").attr("data-bd-id");
	var unitId = $("#community").attr("data-un-id");
	var buildingName = $("#community").attr("data-bd-name");
	var unitName = $("#community").attr("data-un-name");
	var jsarray = new Array();
	var inparam = {
		data : {}
	};
	var isSave = true;
	$(".room-row").each(function(index, element) {
		var roomNbr = $(this).find("input[name='roomNbr']").val();
		if (StrUtil.isEmpty(roomNbr)) {
			$.sucmodal("提示", "房间号不能为空");
			isSave = false;
			return false;
		}
		var ownerName = $(this).find("input[name='ownerName']").val();
		var ownerTel = $(this).find("input[name='ownerTel']").val();
		var floor = $(this).find("input[name='floor']").val();// 所在楼层
		if (!StrUtil.isPositiveInt(floor)) {
			$.sucmodal("提示", "所在楼层应为正整数");
			isSave = false;
			return false;
		}
		//户型
		var buildingTypeId = $(this).find("select[name='type'] option:selected").val();
		
		for ( var i = 0; i < jsarray.length; i++) {
			if (roomNbr == jsarray[i].roomNbr) {
				$.sucmodal("提示", "房间号重复了");
				isSave = false;
				return false;
			}
		}
		var room = {
			building : {},
			roomPartyRel:{}
		};
		var relArray = new Array();
		var partyInfo = {};
		var user = {};
		var roomPartyRel={};
		if (StrUtil.isEmpty(unitId)) {
			room.building.buildingId = buildingId;
		} else {
			room.building.buildingId = unitId;
		}
		room.buildingTypeId = buildingTypeId;
		room.roomNbr = roomNbr;
		room.floor = floor;
		if (!StrUtil.isEmpty(ownerTel) && StrUtil.isEmpty(ownerName)) {
			$.sucmodal("提示", "请填写户主名称");
			isSave = false;
			return false;
		}
		if (!StrUtil.isEmpty(ownerTel)) {
			user.account = ownerTel;
			partyInfo.user = user;
		}
		if (!StrUtil.isEmpty(ownerName)) {
			partyInfo.partyName = ownerName;
			roomPartyRel.partyInfo = partyInfo;
			roomPartyRel.relType='10';
			relArray.push(roomPartyRel);
			room.roomPartyRel = relArray;
		}

		jsarray.push(room);
		// building.buildingName = ;
	});

	if (!isSave) {
		return;
	}

	inparam.data = jsarray;
	$.post("roomAction!saveRoom.action", {
		inParma : JSON.stringify(inparam)
	}, function(data) {
		data = eval("(" + data + ")");
		if (data.result == "true") {
			$.sucmodal("提示", data.msg, function() {
				location.href = "roomManager.jsp";
			});
		} else {
			alert("保存失败" + data.msg);
		}
	});
}

/*
 * function clickOk(){ if
 * (StrUtil.isEmpty($(".proCitySelAll").data("buildingId"))){ $.sucmodal("提示",
 * "请选择楼栋"); return; }
 * 
 * $("#desCompany").text($("#pp_company").val());
 * $("#desCommunity").text($("#community").val()); var buildingId =
 * $(".proCitySelAll").data("buildingId"); var unitId =
 * $(".proCitySelAll").data("unitId"); var buildingName =
 * $(".proCitySelAll").data("buildingName"); var unitName =
 * $(".proCitySelAll").data("unitName"); if (StrUtil.isEmpty(unitId) && unitId !=
 * buildingId){ $("#desBuilding").text(buildingName); }else{
 * $("#desBuilding").text(buildingName+"-"+unitName); } }
 */
function getBuildingTypes(communityId) {
	$.post("parkingAction!getBuildingType.action", {
		"inParma" : JSON.stringify({
			"communityId" : communityId
		})
	}, function(data) {
		var ret = eval("(" + data + ")");
		if (ret.result == "true") {
			var opt = "<option value=''>请选择</option>";
			$(ret.data).each(
					function(ix, ite) {
						opt += "<option value='" + ite.buildingTypeId + "'>"
								+ ite.buildingTypeName + "</option>";
					});
			$("#rooms select").html("");
			$("#rooms select").append(opt);
		} else {
			alert(data.msg);
		}
	});
}
